package com.enterprise.support.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Zhanggaojiang
 * Date: 11-1-19
 * Time: 上午9:26
 * Email: z82422@gmail.com
 * 加载 配置信息文件      config/config.properties
 */

public class Configuration {
    public static String KEY_DATABASE_DRIVER_CLASS_PATH = getProperties("/config/jdbc.properties").getProperty("dbpool.jdbcUrl");

    public static String LibDirectory = "";
    public static String ConfHomeDirectory = "";
    private static Enterprise config = new Enterprise();

    static {
        LibDirectory = com.enterprise.support.utility.Configuration.class.getResource("/config/config.properties").getFile().replace("%20", " ");
        int start = LibDirectory.startsWith("file:") ? 6 : 1;
        int end = 0;
        if (LibDirectory.indexOf("/lib/enterprise.jar") != -1) {
            end = LibDirectory.indexOf("/lib/enterprise.jar");
        } else {
            if (LibDirectory.indexOf("classes") != -1) {
                end = LibDirectory.indexOf("/classes/config/config.properties");
            } else {
                end = LibDirectory.indexOf("/config/config.properties");
            }
        }
        LibDirectory = LibDirectory.substring(start, end);
        ConfHomeDirectory = LibDirectory;
        if (LibDirectory.toUpperCase().endsWith("WEB-INF")) {
            LibDirectory = new File(LibDirectory).getParentFile().getParentFile().getAbsolutePath();
        }
        config = new Enterprise(getProperties("/config/config.properties"));
        //每隔1分钟 检测一次文件更新
        /*ExecuteTask.executeTask(new SchedulerTask(){
            public void run(){
            }
        }, new ScheduleIterator(){
            public Date next() {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MINUTE, 1);
                return calendar.getTime();
            }
        },true);*/
    }

    public static Enterprise get() {
        return config;
    }

    public static Properties getProperties(String filepath) {
        Properties properties = new Properties();
        try {
            properties.load(Configuration.class.getResourceAsStream(filepath));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return properties;
    }

    public static Properties getPropertiesByAbsolutePath(String filepath) {
        Properties properties = new Properties();
        try {
            FileInputStream inputStream = new FileInputStream(filepath);
            properties.load(inputStream);
            inputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return properties;
    }

    public static void store(String file, Properties properties) {
        try {
            FileOutputStream output = new FileOutputStream(file);
            properties.store(output, null);
            output.flush();
            output.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(Configuration.get());
    }
}
